<?php
/**
 * 医生团队页面
 * 州弟医院信息系统 - 州弟学安全
 */

// 包含头文件
include 'includes/header.php';

// 包含数据库连接
require_once 'includes/config.php';

// 获取科室列表（用于筛选）
$dept_sql = "SELECT * FROM departments ORDER BY name";
$dept_result = $conn->query($dept_sql);
$departments = [];
if ($dept_result && $dept_result->num_rows > 0) {
    while ($dept = $dept_result->fetch_assoc()) {
        $departments[$dept['id']] = $dept;
    }
}

// 筛选条件
$dept_filter = isset($_GET['dept']) && is_numeric($_GET['dept']) ? (int)$_GET['dept'] : 0;
$position_filter = isset($_GET['position']) ? $_GET['position'] : '';

// 获取单个医生详情（如果有id参数）
$doctor = null;
$schedules = [];
if (isset($_GET['id']) && is_numeric($_GET['id'])) {
    $doctor_id = (int)$_GET['id'];
    
    // 获取医生信息
    $doc_sql = "SELECT d.*, dep.name as department_name 
                FROM doctors d 
                JOIN departments dep ON d.department_id = dep.id
                WHERE d.id = $doctor_id";
    $doc_result = $conn->query($doc_sql);
    if ($doc_result && $doc_result->num_rows > 0) {
        $doctor = $doc_result->fetch_assoc();
        
        // 获取医生排班
        $sch_sql = "SELECT * FROM doctor_schedules WHERE doctor_id = $doctor_id ORDER BY weekday, start_time";
        $sch_result = $conn->query($sch_sql);
        if ($sch_result && $sch_result->num_rows > 0) {
            while ($sch = $sch_result->fetch_assoc()) {
                $schedules[] = $sch;
            }
        }
    }
}

// 获取医生列表
$doctors = [];
$where_clauses = [];
$where_sql = "";

if ($dept_filter > 0) {
    $where_clauses[] = "d.department_id = $dept_filter";
}

if (!empty($position_filter)) {
    $where_clauses[] = "d.position = '$position_filter'";
}

if (!empty($where_clauses)) {
    $where_sql = "WHERE " . implode(" AND ", $where_clauses);
}

$sql = "SELECT d.*, dep.name as department_name 
        FROM doctors d 
        JOIN departments dep ON d.department_id = dep.id
        $where_sql
        ORDER BY d.position, d.name";
$result = $conn->query($sql);

if ($result && $result->num_rows > 0) {
    while ($row = $result->fetch_assoc()) {
        $doctors[] = $row;
    }
}

// 获取职称列表（用于筛选）
$position_sql = "SELECT DISTINCT position FROM doctors ORDER BY position";
$position_result = $conn->query($position_sql);
$positions = [];
if ($position_result && $position_result->num_rows > 0) {
    while ($pos = $position_result->fetch_assoc()) {
        $positions[] = $pos['position'];
    }
}

// 页面标题
$pageTitle = isset($doctor) ? $doctor['name'] : '医生团队';
?>

<!-- 添加自定义样式解决底部白色区域问题 -->
<style>
.container.my-5 {
    margin-bottom: 0 !important;
    padding-bottom: 2rem;
}
body {
    min-height: 100vh;
    display: flex;
    flex-direction: column;
}
main {
    flex: 1;
}
footer {
    margin-top: auto;
    background-color: #343a40;
}
</style>

<div class="container my-5">
    <?php if (isset($doctor)): ?>
        <!-- 医生详情页 -->
        <div class="mb-4">
            <nav aria-label="breadcrumb">
                <ol class="breadcrumb">
                    <li class="breadcrumb-item"><a href="index.php">首页</a></li>
                    <li class="breadcrumb-item"><a href="doctors.php">医生团队</a></li>
                    <li class="breadcrumb-item active" aria-current="page"><?php echo htmlspecialchars($doctor['name']); ?></li>
                </ol>
            </nav>
        </div>
        
        <div class="row">
            <div class="col-lg-4 mb-4">
                <div class="card">
                    <div class="card-body text-center">
                        <div class="bg-light d-flex justify-content-center align-items-center rounded-circle mx-auto mb-3" style="width: 200px; height: 200px;">
                            <i class="fas fa-user-md fa-5x text-secondary"></i>
                        </div>
                        
                        <h3 class="card-title"><?php echo htmlspecialchars($doctor['name']); ?></h3>
                        <p class="card-text">
                            <span class="badge bg-primary"><?php echo htmlspecialchars($doctor['position']); ?></span>
                            <span class="badge bg-secondary"><?php echo htmlspecialchars($doctor['department_name']); ?></span>
                        </p>
                        
                        <div class="mt-3">
                            <p class="mb-1"><i class="fas fa-phone text-primary me-2"></i> <?php echo htmlspecialchars($doctor['phone'] ?? '暂无'); ?></p>
                        </div>
                        
                        <?php if (isset($_SESSION['logged_in']) && $_SESSION['role'] == 'patient'): ?>
                            <div class="mt-4">
                                <a href="patient/book_appointment.php?doctor_id=<?php echo $doctor['id']; ?>" class="btn btn-primary btn-lg">
                                    <i class="fas fa-calendar-check me-2"></i> 预约挂号
                                </a>
                            </div>
                        <?php else: ?>
                            <div class="mt-4">
                                <a href="login.php" class="btn btn-outline-primary">登录后预约</a>
                            </div>
                        <?php endif; ?>
                    </div>
                </div>
                
                <!-- 出诊安排 -->
                <div class="card mt-4">
                    <div class="card-header bg-primary text-white">
                        <h5 class="mb-0"><i class="fas fa-calendar-alt me-2"></i> 出诊安排</h5>
                    </div>
                    <div class="card-body p-0">
                        <?php if (count($schedules) > 0): ?>
                            <table class="table table-striped mb-0">
                                <thead>
                                    <tr>
                                        <th>星期</th>
                                        <th>时间</th>
                                        <th>状态</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php
                                    $weekdays = ['星期一', '星期二', '星期三', '星期四', '星期五', '星期六', '星期日'];
                                    foreach ($schedules as $schedule): ?>
                                        <tr>
                                            <td><?php echo $weekdays[$schedule['weekday'] - 1]; ?></td>
                                            <td><?php echo substr($schedule['start_time'], 0, 5) . ' - ' . substr($schedule['end_time'], 0, 5); ?></td>
                                            <td>
                                                <?php if ($schedule['is_working']): ?>
                                                    <span class="badge bg-success">出诊</span>
                                                <?php else: ?>
                                                    <span class="badge bg-danger">休息</span>
                                                <?php endif; ?>
                                            </td>
                                        </tr>
                                    <?php endforeach; ?>
                                </tbody>
                            </table>
                        <?php else: ?>
                            <div class="p-3 text-center">暂无排班信息</div>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
            
            <div class="col-lg-8">
                <!-- 个人简介 -->
                <div class="card mb-4">
                    <div class="card-header bg-primary text-white">
                        <h5 class="mb-0"><i class="fas fa-user me-2"></i> 个人简介</h5>
                    </div>
                    <div class="card-body">
                        <?php if (!empty($doctor['bio'])): ?>
                            <p><?php echo nl2br(htmlspecialchars($doctor['bio'])); ?></p>
                        <?php else: ?>
                            <p class="text-muted">暂无详细介绍</p>
                        <?php endif; ?>
                    </div>
                </div>
                
                <!-- 专业特长 -->
                <div class="card mb-4">
                    <div class="card-header bg-primary text-white">
                        <h5 class="mb-0"><i class="fas fa-stethoscope me-2"></i> 专业特长</h5>
                    </div>
                    <div class="card-body">
                        <?php if (!empty($doctor['specialties'])): ?>
                            <p><?php echo nl2br(htmlspecialchars($doctor['specialties'])); ?></p>
                        <?php else: ?>
                            <p class="text-muted">暂无专业特长信息</p>
                        <?php endif; ?>
                    </div>
                </div>
                
                <!-- 教育背景 -->
                <div class="card mb-4">
                    <div class="card-header bg-primary text-white">
                        <h5 class="mb-0"><i class="fas fa-graduation-cap me-2"></i> 教育背景</h5>
                    </div>
                    <div class="card-body">
                        <?php if (!empty($doctor['education'])): ?>
                            <p><?php echo nl2br(htmlspecialchars($doctor['education'])); ?></p>
                        <?php else: ?>
                            <p class="text-muted">暂无教育背景信息</p>
                        <?php endif; ?>
                    </div>
                </div>
                
                <!-- 工作经历 -->
                <div class="card">
                    <div class="card-header bg-primary text-white">
                        <h5 class="mb-0"><i class="fas fa-briefcase me-2"></i> 工作经历</h5>
                    </div>
                    <div class="card-body">
                        <?php if (!empty($doctor['experience'])): ?>
                            <p><?php echo nl2br(htmlspecialchars($doctor['experience'])); ?></p>
                        <?php else: ?>
                            <p class="text-muted">暂无工作经历信息</p>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
        </div>
    <?php else: ?>
        <!-- 医生列表页 -->
        <div class="row mb-5">
            <div class="col-lg-8 mx-auto text-center">
                <h1 class="display-4 text-primary">医生团队</h1>
                <p class="lead text-muted">我院拥有一支专业的医疗团队，为患者提供优质的医疗服务</p>
            </div>
        </div>
        
        <div class="row mb-4">
            <div class="col-12">
                <div class="card">
                    <div class="card-header bg-light">
                        <h5 class="mb-0">筛选条件</h5>
                    </div>
                    <div class="card-body">
                        <form action="doctors.php" method="get" class="row g-3">
                            <div class="col-md-5">
                                <label for="dept">科室</label>
                                <select class="form-select" id="dept" name="dept">
                                    <option value="0">全部科室</option>
                                    <?php foreach ($departments as $id => $dept): ?>
                                        <option value="<?php echo $id; ?>" <?php echo ($dept_filter == $id) ? 'selected' : ''; ?>>
                                            <?php echo htmlspecialchars($dept['name']); ?>
                                        </option>
                                    <?php endforeach; ?>
                                </select>
                            </div>
                            <div class="col-md-5">
                                <label for="position">职称</label>
                                <select class="form-select" id="position" name="position">
                                    <option value="">全部职称</option>
                                    <?php foreach ($positions as $pos): ?>
                                        <option value="<?php echo htmlspecialchars($pos); ?>" <?php echo ($position_filter == $pos) ? 'selected' : ''; ?>>
                                            <?php echo htmlspecialchars($pos); ?>
                                        </option>
                                    <?php endforeach; ?>
                                </select>
                            </div>
                            <div class="col-md-2 d-flex align-items-end">
                                <button type="submit" class="btn btn-primary w-100">筛选</button>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
        
        <div class="row">
            <?php if (count($doctors) > 0): ?>
                <?php foreach ($doctors as $doc): ?>
                    <div class="col-lg-4 col-md-6 mb-4">
                        <div class="card h-100">
                            <div class="card-body text-center">
                                <div class="bg-light d-flex justify-content-center align-items-center rounded-circle mx-auto mb-3" style="width: 150px; height: 150px;">
                                    <i class="fas fa-user-md fa-4x text-secondary"></i>
                                </div>
                                
                                <h4 class="card-title"><?php echo htmlspecialchars($doc['name']); ?></h4>
                                <p class="card-text">
                                    <span class="badge bg-primary"><?php echo htmlspecialchars($doc['position']); ?></span>
                                </p>
                                <p class="card-text text-muted">
                                    <i class="fas fa-hospital-alt me-2"></i><?php echo htmlspecialchars($doc['department_name']); ?>
                                </p>
                                
                                <?php if (!empty($doc['title'])): ?>
                                    <p class="card-text small">
                                        <?php echo htmlspecialchars(mb_substr($doc['title'], 0, 60)) . (mb_strlen($doc['title']) > 60 ? '...' : ''); ?>
                                    </p>
                                <?php endif; ?>
                                
                                <div class="mt-3">
                                    <a href="doctors.php?id=<?php echo $doc['id']; ?>" class="btn btn-outline-primary">查看详情</a>
                                    
                                    <?php if (isset($_SESSION['logged_in']) && $_SESSION['role'] == 'patient'): ?>
                                        <a href="patient/book_appointment.php?doctor_id=<?php echo $doc['id']; ?>" class="btn btn-primary">
                                            <i class="fas fa-calendar-check me-1"></i> 预约
                                        </a>
                                    <?php endif; ?>
                                </div>
                            </div>
                        </div>
                    </div>
                <?php endforeach; ?>
            <?php else: ?>
                <div class="col-12">
                    <div class="alert alert-info">
                        <i class="fas fa-info-circle me-2"></i> 没有找到符合条件的医生
                    </div>
                </div>
            <?php endif; ?>
        </div>
    <?php endif; ?>
</div>

<?php include 'includes/footer.php'; ?> 